package com.easyattendance.model;

import java.util.Comparator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="TBL_CLASS")
public class Class {

	@Id
	@Column(name="class_id",length=15,nullable=false)
	private String class_id;
	
	@Column(name="class_name",length=32)
	private String class_name;
	
	/*@Column(name="common_stud_id")
	private String common_stud_id;
	
	@Column(name="common_stud_pass")
	private String common_stud_pass; */
	
	@Column(name="no_of_electives",length=2)
	private int no_of_electives;
	
	@Column(name="division",length=1,nullable=false)
	private String division;
	
	@Column(name="term_type",nullable=false,length=10)
	private String term_type;
	
	/*@GenericGenerator(name="section_id",strategy="foreign")
	@Column(name="section_id")
	private String section_id;*/
	
	@GenericGenerator(name="teacher_id",strategy="foreign")
	@Column(name="class_teacher_id",length=15,nullable=false)
	private String class_teacher_id; 
	
	/* Many to one mapping of Class */
	@ManyToOne
	@JoinColumn(name="section_id")
	private Section section;
	
	
	/* One to Many mappings of Class */
	
	/*@OneToMany(mappedBy="classObj")
	private Set<AttendanceTakenBy> attendances;
	
	@OneToMany(mappedBy="classObj")
	private Set<StudentAttendance> stud_attendance; */
	
	@OneToMany(mappedBy="classObj", fetch=FetchType.LAZY)
	@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private Set<Subject> subjects;
	
	@OneToMany(mappedBy="classObj", fetch=FetchType.LAZY)
	@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private Set<Student> students;
	
	@OneToMany(mappedBy="classObj", fetch=FetchType.LAZY)
	@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private Set<SubjectClass> subjectClasses;
	
	@OneToMany(mappedBy="classObj", fetch=FetchType.LAZY)
	@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private Set<Assignment> assignments;
	
	@OneToMany(mappedBy="classObj", fetch=FetchType.LAZY)
	@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private Set<MaterialClass> materialClass;
	
	/* one to one mappings of Class */
  /*	@OneToOne(optional=false,fetch=FetchType.LAZY)
	@PrimaryKeyJoinColumn
	private Teacher classTeacher; */
	
	@OneToOne(mappedBy="classObj", fetch=FetchType.LAZY)
	@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private ClassUser class_users;
	
	/* @ManyToMany(cascade={CascadeType.ALL})
	@JoinTable(name="SemYear_Subject",joinColumns={@JoinColumn(name="sem_year_id")},inverseJoinColumns={@JoinColumn(name="subject_id")})
	private Set<Subject> sem_subject=new HashSet<Subject>(); */
	
	public Class() {
		super();
	}
	
	public Class(String class_id, String class_name, int no_of_electives,String division) {
		super();
		this.class_id = class_id;
		this.class_name = class_name;
		this.no_of_electives = no_of_electives;
		this.division = division;
		//this.section_id = section_id;
		//this.class_teacher_id = class_teacher_id;
	}



	public Class(String class_id,Section section, Teacher teacher,
			Set<Subject> subjects, Set<Student> students,
			Set<SubjectClass> subjectClasses, Set<Assignment> assignments,
			Set<MaterialClass> materialClass, String class_name,
			int no_of_electives) {
		super();
		this.class_id = class_id;
		this.section = section;
		this.subjects = subjects;
		this.students = students;
		this.subjectClasses = subjectClasses;
		this.assignments = assignments;
		this.materialClass = materialClass;
		
		this.class_name = class_name;
		this.no_of_electives = no_of_electives;
		
	}
	
	

	public Class(String class_id, String class_name, int no_of_electives,
			String division, String class_teacher_id) {
		super();
		this.class_id = class_id;
		this.class_name = class_name;
		this.no_of_electives = no_of_electives;
		this.division = division;
		this.class_teacher_id = class_teacher_id;
		
	}

	public Class(String class_id, String class_name, int no_of_electives,
			String division, String term_type, String class_teacher_id) {
		super();
		this.class_id = class_id;
		this.class_name = class_name;
		this.no_of_electives = no_of_electives;
		this.division = division;
		this.term_type = term_type;
		this.class_teacher_id = class_teacher_id;
	}

	public Class(String class_id, String class_name,
			int no_of_electives) {
		super();
		this.class_id = class_id;
		this.class_name = class_name;
		this.no_of_electives = no_of_electives;
	}

	public Class(String class_id) {
		super();
		this.class_id = class_id;
	}

	public String getClass_id() {
		return class_id;
	}

	public void setClass_id(String class_id) {
		this.class_id = class_id;
	}

	
	public String getDivision() {
		return division;
	}

	public void setDivision(String division) {
		this.division = division;
	}

	
	public Set<Subject> getSubjects() {
		return subjects;
	}

	public void setSubjects(Set<Subject> subjects) {
		this.subjects = subjects;
	}

	public Set<Student> getStudents() {
		return students;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}

	public Set<SubjectClass> getSubjectClasses() {
		return subjectClasses;
	}

	public void setSubjectClasses(Set<SubjectClass> subjectClasses) {
		this.subjectClasses = subjectClasses;
	}

	public Set<Assignment> getAssignments() {
		return assignments;
	}

	public void setAssignments(Set<Assignment> assignments) {
		this.assignments = assignments;
	}

	

/*	public Teacher getClassTeacher() {
		return classTeacher;
	}

	public void setClassTeacher(Teacher classTeacher) {
		this.classTeacher = classTeacher;
	}   */
 
	public Set<MaterialClass> getMaterialClass() {
		return materialClass;
	}



	public void setMaterialClass(Set<MaterialClass> materialClass) {
		this.materialClass = materialClass;
	}



	public String getClass_name() {
		return class_name;
	}

	public void setClass_name(String class_name) {
		this.class_name = class_name;
	}

	public int getNo_of_electives() {
		return no_of_electives;
	}

	public void setNo_of_electives(int no_of_electives) {
		this.no_of_electives = no_of_electives;
	}

	

	public String getClass_teacher_id() {
		return class_teacher_id;
	}



	public void setClass_teacher_id(String class_teacher_id) {
		this.class_teacher_id = class_teacher_id;
	}



	public Section getSection() {
		return section;
	}

	public void setSection(Section section) {
		this.section = section;
	}

	public ClassUser getClass_users() {
		return class_users;
	}

	public void setClass_users(ClassUser class_users) {
		this.class_users = class_users;
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((class_id == null) ? 0 : class_id.hashCode());
		result = prime
				* result
				+ ((class_teacher_id == null) ? 0 : class_teacher_id.hashCode());
		return result;
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Class other = (Class) obj;
		if (class_id == null) {
			if (other.class_id != null)
				return false;
		} else if (!class_id.equals(other.class_id))
			return false;
		if (class_teacher_id == null) {
			if (other.class_teacher_id != null)
				return false;
		} else if (!class_teacher_id.equals(other.class_teacher_id))
			return false;
		return true;
	}

}

